
// Tencent is pleased to support the open source community by making Mars available.
// Copyright (C) 207 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the MIT License (the "License"); you may not use this file except in 
// compliance with the License. You may obtain a copy of the License at
// http://opensource.org/licenses/MIT
// Unless required by applicable law or agreed to in writing, software distributed under the License is
// distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions and
// limitations under the License.
/*
 * appender-swift-bridge.mm
 *
 *  Created on: 2017-1-3
 *      Author: Jinkey
 */

#import "appender-swift-bridge.h"
#import <mars/xlog/appender.h>
#import <mars/xlog/xlogger.h>
#import <mars/xlog/xloggerbase.h>

#import <sys/xattr.h>

@implementation JinkeyMarsBridge

static const char* emptyStr = "";


static QWLogAllLogCallBack g_LogCallBack = nil;

+ (QWLogAllLogCallBack)logCallBack{
    return g_LogCallBack;
}

+ (void)setLogCallBack:(QWLogAllLogCallBack)logCallBack{
    g_LogCallBack = logCallBack;
}

+ (void)unint {
    appender_close();
}

// 封装了初始化 Xlogger 方法
// initialize Xlogger
-(void)initXlogger: (XloggerType)debugLevel releaseLevel: (XloggerType)releaseLevel path: (NSString*)path prefix: (const char*)prefix key:(const char*)key{




    NSString* logPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:path];
    if (path.length<1) {
        path = logPath;
    }

    const char* attrName = "io.jinkey";
    u_int8_t attrValue = 1;
    setxattr([path UTF8String], attrName, &attrValue, sizeof(attrValue), 0, 0);

    // init xlog
    #if DEBUG

    xlogger_SetLevel((TLogLevel)debugLevel);
    appender_set_console_log(true);
    #else

    xlogger_SetLevel((TLogLevel)releaseLevel);
    appender_set_console_log(false);
    #endif

    appender_open(kAppednerAsync, [path UTF8String], prefix, key);



}

+ (void)appenderFlush:(BOOL)flush{
     appender_flush_sync();
}

// 封装了关闭 Xlogger 方法
// deinitialize Xlogger
-(void)deinitXlogger {
    appender_close();
}


// 利用微信提供的 LogUtil.h 封装了打印日志的方法
// print log using LogUtil.h provided by Wechat
+(void) log: (XloggerType) level tag:(NSString *)tag message: (NSString*)message{

    NSString* levelDescription = @"";

    switch (level) {
        case debug:
//            LOG_DEBUG(tag, content);
            levelDescription = @"Debug";
            break;
        case info:
//            LOG_INFO(tag, content);
            levelDescription = @"Info";
            [self debug:tag msg:@"%@", message];
            break;
        case warning:
//            LOG_WARNING(tag, content);
            levelDescription = @"Warn";
            [self info:tag msg:@"%@", message];
            break;
        case error:
//            LOG_ERROR(tag, content);
            levelDescription = @"Error";
            [self error:tag msg:@"%@", message];
            break;
        default:
            break;
    }

}

static NSString *xlog_formatLogStr(NSString *tag, NSString *format, va_list args) {
    NSString *input = [[NSString alloc] initWithFormat:format arguments:args];
    NSString *logString = [NSString stringWithFormat:@"%@] %@", tag, input];
    return logString;
}

void __xlog_log(TLogLevel level,NSString *tag,NSString *format,va_list args) {
    NSString *log = xlog_formatLogStr(tag, format, args);
    TLogLevel tLevel;
    tLevel = (TLogLevel)level;
    xlogger2(tLevel,emptyStr,emptyStr,emptyStr,0,log.UTF8String);
//    if(JinkeyMarsBridge.logCallBack) XLog.logCallBack(level,log);
}


+ (void)verbose:(NSString *)tag msg:(NSString *)format, ...NS_FORMAT_FUNCTION(2, 3) {
    va_list args;
    va_start(args, format);
    __xlog_log(kLevelVerbose,tag, format, args);
    va_end(args);
}

+ (void)debug:(NSString *)tag msg:(NSString *)format, ...NS_FORMAT_FUNCTION(2, 3) {

    va_list args;
    va_start(args, format);
    __xlog_log(kLevelDebug,tag, format, args);
    va_end(args);
}

+ (void)info:(NSString *)tag msg:(NSString *)format, ...NS_FORMAT_FUNCTION(2, 3) {
    va_list args;
    va_start(args, format);
    __xlog_log(kLevelInfo,tag, format, args);
    va_end(args);
}

+ (void)warn:(NSString *)tag msg:(NSString *)format, ...NS_FORMAT_FUNCTION(2, 3) {
    va_list args;
    va_start(args, format);
    __xlog_log(kLevelWarn,tag, format, args);
    va_end(args);
}

+ (void)error:(NSString *)tag msg:(NSString *)format, ...NS_FORMAT_FUNCTION(2, 3) {
    va_list args;
    va_start(args, format);
    __xlog_log(kLevelError,tag, format, args);
    va_end(args);
}


+ (void)info:(NSString *)tag message:(NSString*)message{
    [self info:tag msg:@"%@", message];
}

+ (void)debug:(NSString *)tag message:(NSString*)message{
    [self debug:tag msg:@"%@", message];
}

+ (void)warn:(NSString *)tag message:(NSString*)message{
    [self warn:tag msg:@"%@", message];
}

+ (void)error:(NSString *)tag message:(NSString*)message{
    [self error:tag msg:@"%@", message];
}

+ (void)verbose:(NSString *)tag message:(NSString*)message{
    [self verbose:tag msg:@"%@", message];
}


@end


